import 'package:enterprise_v6_app/app/components/common/custom_no_data.dart';
import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_intrinsic_circle_line.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_task_instructions_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_task_process_nodes_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/common/drill_process/drill_process_controller.dart';

/// 通用的演练流程页面
/// [isEnabled] 是否可编辑,默认为否
class DrillProcessPage extends StatelessWidget {
  DrillProcessPage({super.key, this.isEnabled = false, this.exercisePlanId});

  /// 演练计划id
  final String? exercisePlanId;
  final bool isEnabled;
  final controller = Get.put(DrillProcessController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        initState: (state) {
          controller.initData(exercisePlanId);
        },
        builder: (_) {
          return Container(
            alignment: Alignment.topCenter,
            color: const Color(0xffffffff),
            padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.w),
            child: Column(
              children: [
                // CustomSearchEnter(
                //   height: 32.w,
                //   hintText: '请输入节点名称',
                //   text: controller.search,
                //   onCallBack: (value) {
                //     controller.search = value;
                //     controller.getExercisePlanList();
                //   },
                // ),
                // AppGaps.vGap20,
                Expanded(
                    child: controller.nodeProcessList.isEmpty
                        ? const CustomNoData()
                        : ListView.separated(
                            itemBuilder: (context, index) {
                              final view = controller.nodeProcessList[index];
                              return _getTaskWidget(view);
                            },
                            separatorBuilder: (context, index) {
                              return AppGaps.vGap20;
                            },
                            itemCount: controller.nodeProcessList.length,
                          ))
              ],
            ),
          );
        });
  }

  /// 获取任务列显示
  Widget _getTaskWidget(EmergencyTaskProcessNodesEntity item) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Container(
              height: 14.w,
              width: 3.w,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(topRight: Radius.circular(90.w), bottomLeft: Radius.circular(20.w), bottomRight: Radius.circular(20.w)),
                gradient: LinearGradient(
                    //渐变位置
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    //渐变颜色[始点颜色, 结束颜色]
                    colors: [const Color(0xff587DF5), const Color(0xff587DF5), const Color(0xff587DF5).withOpacity(0.6)]),
              ),
            ),
            AppGaps.hGap10,
            Expanded(
              child: Text(
                item.nodeName,
                style: TextStyle(
                  fontSize: 16.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w500,
                  color: const Color(0xff262F38),
                ),
              ),
            ),
            Row(
              children: [
                Text(
                  item.isExpanded ? '收起' : '展开',
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.2,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xff5C6DFF),
                  ),
                ),
                AppGaps.hGap6,
                SvgPicture.asset(
                  item.isExpanded ? AppImages.icon_circle_up : AppImages.icon_circle_down,
                  colorFilter: const ColorFilter.mode(Color(0xff5C6DFF), BlendMode.srcIn),
                  width: 16.w,
                  height: 16.w,
                )
              ],
            ).clickOnce(() {
              item.isExpanded = !item.isExpanded;
              controller.update();
            }),
          ],
        ),
        AppGaps.vGap16,
        Container(
          alignment: Alignment.topCenter,
          padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 14.w),
          decoration: BoxDecoration(
            color: const Color(0xffffffff),
            border: Border.all(
              color: const Color(0xffDEE2FF),
              width: 1.w,
            ),
            borderRadius: BorderRadius.circular(4.r),
          ),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    '流程任务描述:',
                    style: TextStyle(
                      fontSize: 16.sp,
                      height: 1.2,
                      fontWeight: FontWeight.w400,
                      color: const Color(0xff262F38),
                    ),
                  ),
                  Visibility(
                    visible: isEnabled || item.isSelected,
                    child: Container(
                      width: 30.w,
                      height: 30.w,
                      color: Colors.transparent,
                      alignment: Alignment.center,
                      child: SvgPicture.asset(
                        item.isSelected ? AppImages.icon_selection_box : AppImages.icon_un_selection_box,
                        width: 16.w,
                        height: 16.w,
                      ),
                    ).clickOnce(() {
                      if (!isEnabled) {
                        return;
                      }
                      controller.selectTaskView(item);
                    }),
                  ),
                ],
              ),
              AppGaps.vGap10,
              Container(
                alignment: Alignment.topLeft,
                padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.w),
                decoration: BoxDecoration(
                  color: const Color(0xffF7F7FB),
                  border: Border.all(
                    color: const Color(0xffEBEDF0),
                    width: 1.w,
                  ),
                  borderRadius: BorderRadius.circular(4.r),
                ),
                child: Text(
                  item.nodeDescription,
                  style: TextStyle(
                    fontSize: 13.sp,
                    height: 1.2,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xff495061),
                  ),
                ),
              ),
            ],
          ),
        ),
        AppGaps.vGap16,
        AnimatedSize(
          duration: const Duration(milliseconds: 500),
          curve: Curves.easeInOut,
          // curve: Curves.easeInOutExpo,
          child: Container(
            height: item.isExpanded ? null : 0,
            padding: EdgeInsets.symmetric(horizontal: 2.w),
            child: ListView.builder(
              shrinkWrap: true,
              physics: const BouncingScrollPhysics(),
              itemCount: item.emergencyTaskInstructions.length,
              itemBuilder: (BuildContext context, int index) {
                final bean = item.emergencyTaskInstructions[index];
                return CustomIntrinsicCircleLine(
                  iconSpaceHeight: 0.w,
                  showWidget: Container(
                    margin: EdgeInsets.only(bottom: 20.w),
                    child: _getNodeView(index: index, parentBean: item, bean: bean),
                  ),
                );
              },
            ),
          ),
        ),
      ],
    );
  }

  /// 子节点样式
  /// [parentBean] 父节点
  /// [bean]当前节点
  Widget _getNodeView({
    required int index,
    required EmergencyTaskProcessNodesEntity parentBean,
    required EmergencyTaskInstructionsEntity bean,
  }) {
    return Container(
      alignment: Alignment.topLeft,
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 16.w),
      decoration: BoxDecoration(
        color: const Color(0xffF7F7FB),
        border: Border.all(
          color: const Color(0xffEBEDF0),
          width: 1.w,
        ),
        borderRadius: BorderRadius.circular(4.r),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: const Color(0xff5C6DFF),
              borderRadius: BorderRadius.circular(8.r),
            ),
            width: 16.w,
            height: 16.w,
            child: Text(
              '${index + 1}',
              style: TextStyle(
                fontSize: 10.sp,
                height: 1.2,
                fontWeight: FontWeight.w500,
                color: const Color(0xffffffff),
              ),
            ),
          ),
          AppGaps.hGap10,
          Expanded(
            child: Column(
              children: [
                CustomTextWidget.getTextRowView(title: '任务指令', value: bean.taskInstructions),
                AppGaps.vGap12,
                CustomTextWidget.getTextRowView(title: '执行人', value: bean.taskUserList.map((e) => e.userName).join(',')),
                Visibility(
                    visible: bean.whetherToExecute == '1',
                    child: Column(
                      children: [
                        AppGaps.vGap12,
                        CustomTextWidget.getTextRowView(title: '执行时间', value: bean.updateDate),
                      ],
                    ))
              ],
            ),
          ),
          Visibility(
            visible: isEnabled || (bean.whetherToExecute == '1'),
            child: Container(
              width: 30.w,
              height: 30.w,
              color: Colors.transparent,
              alignment: Alignment.topRight,
              child: SvgPicture.asset(
                bean.whetherToExecute == '1' ? AppImages.icon_selection_box : AppImages.icon_un_selection_box,
                width: 16.w,
                height: 16.w,
              ),
            ),
          ),
        ],
      ),
    ).clickOnce(() {
      if (!isEnabled) {
        return;
      }
      controller.selectNodeView(parentBean, bean);
    });
  }
}
